package com.huifaapp.mall.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.HotSearchClassify;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.uitils.Contig;
import com.huifaapp.mall.uitils.DimenUtils;
import com.huifaapp.mall.view.HotSearchFlowLayout;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashSet;
import java.util.List;

/**
 * Created by DM on 2018/1/26.
 */
@SuppressLint("ValidFragment")
public class HotSearchFragment extends Fragment {
    private String fragmentTitle;
    private Context context;
    private View view;;
    private HotSearchFlowLayout hotSearchFlowLayout;
    private HotSearchFlowLayout Historical_search_content_hsfl;
    private TextView Historical_search_title_tv;
    private List<HotSearchClassify.HotSearchClassifySub.Label> labels;
    private TextView tv_cleanSear;
    private int isHighLight;

    public HotSearchFragment(String fragmentTitle)
    {
        this.fragmentTitle = fragmentTitle;
    }

    public String getFragmentTitle() {
        return fragmentTitle;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        view = inflater.inflate(R.layout.fragment_hot_search,container,false);
        initView(inflater,container,savedInstanceState);
        initData();
        return view;
    }

    /**
     * 热门搜索网络请求
     */
    private void initData() {

        String hotSearchClassifyUrl = ApiManager.BASE_NIUPAI_URL+"goods/wordTags";
        OkHttpUtils
                .post()
                .url(hotSearchClassifyUrl)
                .addParams("","")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        HotSearchClassify hotSearchClassify = gson.fromJson(response,HotSearchClassify.class);
                        if(hotSearchClassify.getCode().equals("10000")) {
                            HotSearchClassify.HotSearchClassifySub hotSearchClassifySub =
                                    hotSearchClassify.getData();
                            if (getFragmentTitle().equals("友惠")) {
                                labels = hotSearchClassifySub.get友惠();
                            } else if (getFragmentTitle().equals("京东")) {
                                labels = hotSearchClassifySub.get京东();
                            } else {
                                labels = hotSearchClassifySub.get全部();
                            }
                            displayUI();
                            displayUI1();
                        }else{
                            Toast.makeText(getActivity(),hotSearchClassify.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        hotSearchFlowLayout = (HotSearchFlowLayout) view.findViewById(R.id.hot_search_content_hsfl);
        Historical_search_content_hsfl= (HotSearchFlowLayout) view.findViewById(R.id.Historical_search_content_hsfl);
        Historical_search_title_tv= (TextView) view.findViewById(R.id.Historical_search_title_tv);
        tv_cleanSear= (TextView) view.findViewById(R.id.tv_cleanSear);
        if(Contig.hos.size()>0){
            tv_cleanSear.setVisibility(View.VISIBLE);
        }else{
            tv_cleanSear.setVisibility(View.GONE);
        }
        tv_cleanSear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_cleanSear.setVisibility(View.GONE);
                Historical_search_content_hsfl.setVisibility(View.GONE);
                Contig.hos.clear();
                displayUI1();

            }
        });
    }
    private void displayUI() {
        for (int i = 0; i < labels.size(); i++) {
            final String word = labels.get(i).getWord();
            if(labels.get(i).getIsHighLight().equals("")){
                isHighLight=0;
            }else {
                isHighLight = Integer.parseInt(labels.get(i).getIsHighLight());
            }
            final TextView tv = new TextView(context);
            tv.setWidth(DimenUtils.dip2px(context,45));
            tv.setHeight(DimenUtils.dip2px(context,25));
            tv.setText(word);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            tv.setGravity(Gravity.CENTER);
            int paddingy = DimenUtils.dip2px(context, 5);
            int paddingx =  DimenUtils.dip2px(context, 4);
            tv.setPadding(paddingx, paddingy, paddingx, paddingy);
            tv.setClickable(false);
            if(isHighLight == 1)
            {
                tv.setBackgroundDrawable(createShape(Color.RED));
                tv.setTextColor(Color.RED);
            }
            else
            {
                tv.setBackgroundDrawable(createShape(Color.rgb(32,32,32)));
                tv.setTextColor(Color.rgb(32,32,32));
            }
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), Search_showActivity.class);
                    intent.putExtra("keyword", tv.getText().toString());
                    intent.putExtra("titlename","查看列表");
                    startActivity(intent);
                }
            });
            hotSearchFlowLayout.addView(tv);
        }
    }
    private void displayUI1() {

        if(Contig.hos.size()==0){
            Historical_search_title_tv.setVisibility(View.GONE);
        }else {
            Historical_search_title_tv.setVisibility(View.VISIBLE);
            for (int i = 0; i < Contig.hos.size(); i++) {
                final String word = Contig.hos.get(i);
                final TextView tv = new TextView(context);
                tv.setWidth(DimenUtils.dip2px(context, 45));
                tv.setHeight(DimenUtils.dip2px(context, 25));
                tv.setText(word);
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                tv.setGravity(Gravity.CENTER);
                int paddingy = DimenUtils.dip2px(context, 5);
                int paddingx = DimenUtils.dip2px(context, 4);
                tv.setPadding(paddingx, paddingy, paddingx, paddingy);
                tv.setClickable(false);
//            if(isHighLight == 1)
//            {
//                tv.setBackgroundDrawable(createShape(Color.RED));
//                tv.setTextColor(Color.RED);
//            }
//            else
//            {
                tv.setBackgroundDrawable(createShape(Color.rgb(32, 32, 32)));
                tv.setTextColor(Color.rgb(32, 32, 32));
//            }
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), Search_showActivity.class);
                        intent.putExtra("keyword", tv.getText().toString());
                        intent.putExtra("titlename", "查看列表");
                        startActivity(intent);
                    }
                });
                Historical_search_content_hsfl.addView(tv);
            }
        }
    }

//    @Override
//    public void onStart() {
//        super.onStart();
////        displayUI1();
//    }



    public GradientDrawable createShape(int strokeColor){
        GradientDrawable drawable=new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadius(DimenUtils.dip2px(context,15));//设置4个角的弧度
        drawable.setStroke(DimenUtils.dip2px(context,1),strokeColor);// 设置颜色
        return drawable;
    }
}
